import json
import time
from tqdm import tqdm
import requests
TOKEN = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwYXlsb2FkIjp7InJ0IjoxNzM2MTczMTI4LCJkYXRhIjoiOTdkZTI2ZmI4Y2IyNDdhODhiYjA2NDIwODJiNjA3NzAifSwiZXhwIjoxNzM1NjU0NzI4fQ.2GTe6RDMLHVOQukTjj6iY4mHYjlyUCB9Ni9Br_TdY8Q"
headers = {
    "Authorization": TOKEN,
    "Content-Type": "application/json",
    "Host": "funhpc.com",
    "Origin": "https://www.funhpc.com",
    "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0'
}

def answer_questions():
    with open('question.json', 'r') as f:
        data = json.load(f)
    data = data['data']
    with open('all_right_question.json', 'r') as f:
        right = json.load(f)
    body = {
        "id":"ed9b918c-29dd-4eab-b017-b88a74437698",
        "questionsId":"28e4ae34-3b59-4e38-810e-b335b9c30b18",
        "myAnswer":"A,B,C,D",
        "rightAnswer":"A,B,C,D",
        "isRight":"1"
    }
    url = "http://10.66.8.201/api/edu/v1/edu/questions/user/save"

    for index, item in enumerate(right):
        item = item['data']
        body['id'] = data[index]['id']
        body['questionsId'] = item['id']
        eduQuestionsOptionsList = item['eduQuestionsOptionsList']
        a = []
        for answer in eduQuestionsOptionsList:
            if answer['rightAnswers'] == "1":
                a.append(answer['optionKey'])
        body['myAnswer'] = ','.join(a)
        body['rightAnswer'] = ','.join(a)
        response = requests.post(url, headers=headers, json=body)
        if response.status_code == 200:
            print("success")

def study_course():
    with open('course.json', 'r', encoding='utf-8') as f:
        data = json.load(f)
    data = data['data']['rows']
    body = {
        "courseId": "46f51cd5-37c0-459e-8e58-18a8846a6ce3",
        "learnedTime": 417
    }
    url = "http://10.66.8.201/api/edu/v1/edu/course/user/save"

    for item in data:
        body['courseId'] = item['id']
        body['learnedTime'] = item['learnSize']
        response = requests.post(url, headers=headers, json=body)
        time.sleep(2)
        if response.status_code == 200:
            print("success")

def get_eaxm():
    with open('all_right_question.json', 'r', encoding='utf-8') as f:
        question = json.load(f)
    with open('exam.json', 'r', encoding='utf-8') as f:
        exam = json.load(f)
    exam = exam['data']
    for index, item in enumerate(exam):
        for q in question:
            q = q['data']
            if item['id'] == q['id']:
                eduQuestionsOptionsList = q['eduQuestionsOptionsList']
                a = []
                for answer in eduQuestionsOptionsList:
                    if answer['rightAnswers'] == "1":
                        a.append(answer['optionKey'])
                print(f"({index + 1}, {','.join(a)})", end='  ')
                break
        if (index + 1) % 5 == 0:
            print()
def get_all_right_answer():
    all_data = []
    url = 'http://10.66.8.201/api/edu/v1/edu/questions/get?id='
    with open('question.json', 'r') as f:
        data = json.load(f)
    data = data['data']
    for item in tqdm(data):
        question_id = item['questionsId']
        temp = url + question_id
        response = requests.get(temp, headers=headers)
        json_data = response.json()
        all_data.append(json_data)
    with open('all_right_question', 'w', encoding='utf-8') as f:
        json.dump(all_data, f, indent=4)

def format():
    with open('all_right_question.json', 'r') as f:
        data = json.load(f)
    new_all_data = []
    for i, item in enumerate(data):
        item = item['data']
        item['name'] = item['name'].encode().decode('unicode_escape')
        eduQuestionsOptionsList = item['eduQuestionsOptionsList']
        for j, a in enumerate(eduQuestionsOptionsList):
            eduQuestionsOptionsList[j]['optionValue'] = eduQuestionsOptionsList[j]['optionValue'].encode().decode('unicode_escape')
        item['eduQuestionsOptionsList'] = eduQuestionsOptionsList
        new_all_data.append(item)
    with open('all_right_question', 'w', encoding='utf-8') as f:
        json.dump(new_all_data, f, indent=4)

if __name__ == '__main__':
    answer_questions()